#include "myplaylistsongsview.h"
#include "ui_myplaylistsongsview.h"
#include "commonModel/songmodel.h"
#include "playListModel/myplaylistmodel.h"
#include <QLabel>
#include "playEngine/ezvoiceplayermanager.h"
#include "common/songMenu/songmenu.h"

MyPlayListSongsView::MyPlayListSongsView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyPlayListSongsView)
{
    _myPlaylist = 0;
    ui->setupUi(this);
    connect(ui->tableWidget,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(cellDoubleClicked(int,int)));

    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->tableWidget,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(slot_customContextMenuRequested(const QPoint&)));

}

MyPlayListSongsView::~MyPlayListSongsView()
{
    delete ui;
}
MyPlayListModel *MyPlayListSongsView::myPlaylist() const
{
    return _myPlaylist;
}

void MyPlayListSongsView::refreshView()
{
    ui->tableWidget->clear();
    if(_myPlaylist==0){
        return ;
    }
    ui->tableWidget->setRowCount(_myPlaylist->count());
    ui->tableWidget->setColumnCount(1);
    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width());
    for(int i=0;i<_myPlaylist->count();i++){
        SongModel *song = _myPlaylist->songAtIndex(i);
        QLabel *label = new QLabel(ui->tableWidget);
        label->setText(song->title());
        ui->tableWidget->setCellWidget(i,0,label);
    }
}

void MyPlayListSongsView::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
//    ui->tableWidget->resizeColumnToContents(0);
    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width());
}


void MyPlayListSongsView::setMyPlaylist(MyPlayListModel *myPlaylist)
{
    if(myPlaylist){
        myPlaylist->Retain();
    }
    if(_myPlaylist){
        _myPlaylist->Release();
    }
    _myPlaylist = myPlaylist;

    refreshView();
}

void MyPlayListSongsView::cellDoubleClicked(int row, int)
{
    int index = row;

    EZVoicePlayerManager *playerManager = EZVoicePlayerManager::defaultManager();
    PlayListBaseModel *oldPlayLlist = playerManager->getPlayList();
    if(oldPlayLlist==_myPlaylist){
//        QVariant nowPlayPath = playerManager->getPlayList()->getUserData(QString("playListPath"));
        playerManager->playIndex(index);
        return;
    }


//    NormalPlayListModel *playList = new NormalPlayListModel();
//    playList->setUserData(QString("playListPath"),QVariant(_path));
//    for(int i=0;i<_fileNames.count();i++){
//        QString songPath = _path +"/" + _fileNames.at(i);
//        SongModel *song = new SongModel();
////        song->setFilePath(songPath);
//        SongModel::Quality quality;
//        quality.url = songPath;
//        QString key(songHash_localPath);
//        song->setQualityWithKey(key,quality);
//        song->setTitle(_fileNames.at(i));
//        playList->addSong(song);
//        song->Release();
//    }

    playerManager->setPlayList(_myPlaylist);
    playerManager->playIndex(index);
//    playList->Release();
}

void MyPlayListSongsView::slot_customContextMenuRequested(const QPoint & pos)
{
    int row = ui->tableWidget->rowAt(pos.y());
    if(row < 0){
        return;
    }

    QPoint point(pos.x(),ui->tableWidget->horizontalHeader()->height()+pos.y());

//    QString songPath = _path +"/" + _fileNames.at(row);


    QMenu menu;
//    menu.addMenu()
    SongAddToMyPlayListMenu spm;

    QAction *addMenuAction = menu.addMenu(&spm);
    addMenuAction->setText("add to myPlayList");
    QAction *deleteAction = menu.addAction("delete song");

    QAction *selectAction = menu.exec(ui->tableWidget->mapToGlobal(point));
    if(selectAction==deleteAction){
        _myPlaylist->removeSong(_myPlaylist->songAtIndex(row),true);
        refreshView();
    }else{

       spm.setSong(_myPlaylist->songAtIndex(row));

        spm.runActionEvent(selectAction);

    }
}


